package org.javaforever.gatescore.action;

import java.io.FileInputStream;
import java.io.InputStream;
import java.io.Serializable;
import java.util.TreeSet;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.javaforever.gatescore.core.FrontProject;
import org.javaforever.gatescore.poi.SpreadSheetTranslater;
import org.javaforever.gatescore.utils.StringUtil;

public class ParseSpreadSheetToProjectAction implements Cloneable,Serializable{
	private static final long serialVersionUID = 6540180022628997630L;

	public FrontProject parseProject(String folderPath,String fileName) throws Exception{
		InputStream is = new FileInputStream(folderPath+fileName);
		POIFSFileSystem fs = new POIFSFileSystem(is);
		HSSFWorkbook wb = new HSSFWorkbook(fs);
		
		SpreadSheetTranslater translater = new SpreadSheetTranslater();
		FrontProject fProject = translater.translate(wb);
		fProject.setExcelTemplateFolder(folderPath);
		fProject.setExcelTemplateName(fileName);
		return fProject;
	}
	
	public FrontProject parseMjeeeuProject(String folderPath,String fileName) throws Exception{
		InputStream is = new FileInputStream(folderPath+fileName);
		POIFSFileSystem fs = new POIFSFileSystem(is);
		HSSFWorkbook wb = new HSSFWorkbook(fs);
		
		SpreadSheetTranslater translater = new SpreadSheetTranslater();
		FrontProject fProject = translater.mjeeeuTranslate(wb);
		fProject.setExcelTemplateFolder(folderPath);
		fProject.setExcelTemplateName(fileName);
		return fProject;
	}
	
	public FrontProject parseGoProject(String folderPath,String fileName) throws Exception{
		InputStream is = new FileInputStream(folderPath+fileName);
		POIFSFileSystem fs = new POIFSFileSystem(is);
		HSSFWorkbook wb = new HSSFWorkbook(fs);
		
		SpreadSheetTranslater translater = new SpreadSheetTranslater();
		FrontProject fProject = translater.goTranslate(wb);
		fProject.setExcelTemplateFolder(folderPath);
		fProject.setExcelTemplateName(fileName);
		return fProject;
	}
	
	public FrontProject parseRustProject(String folderPath,String fileName) throws Exception{
		InputStream is = new FileInputStream(folderPath+fileName);
		POIFSFileSystem fs = new POIFSFileSystem(is);
		HSSFWorkbook wb = new HSSFWorkbook(fs);
		
		SpreadSheetTranslater translater = new SpreadSheetTranslater();
		FrontProject fProject = translater.rustTranslate(wb);
		fProject.setExcelTemplateFolder(folderPath);
		fProject.setExcelTemplateName(fileName);
		return fProject;
	}
	
	public FrontProject parseZigProject(String folderPath,String fileName) throws Exception{
		InputStream is = new FileInputStream(folderPath+fileName);
		POIFSFileSystem fs = new POIFSFileSystem(is);
		HSSFWorkbook wb = new HSSFWorkbook(fs);
		
		SpreadSheetTranslater translater = new SpreadSheetTranslater();
		FrontProject fProject = translater.zigTranslate(wb);
		fProject.setExcelTemplateFolder(folderPath);
		fProject.setExcelTemplateName(fileName);
		return fProject;
	}
	
	public static String doParseExcel(String excelPath,String excelFileName, String templatesPath,String savePath,boolean showBackendProject,boolean useControllerPrefix, String exportStr, String originalFileName) throws Exception {
		return doParseExcel(excelPath,excelFileName,templatesPath,savePath,showBackendProject,false,useControllerPrefix,exportStr,originalFileName);
	}
	
	public static String doParseMjeeeuExcel(String excelPath,String excelFileName, String templatesPath,String savePath,boolean showBackendProject,boolean useControllerPrefix, String exportStr, String originalFileName) throws Exception {
		return doParseMjeeeuExcel(excelPath,excelFileName,templatesPath,savePath,showBackendProject,false,useControllerPrefix,exportStr,originalFileName);
	}
	
	public static String doParseExcel(String excelPath,String excelFileName, String templatesPath,String savePath,boolean showBackendProject,boolean useController,boolean useControllerPrefix, String exportStr, String originalFileName) throws Exception {
		ParseSpreadSheetToProjectAction action = new ParseSpreadSheetToProjectAction();
		FrontProject project = action.parseProject(excelPath,excelFileName);
		project.setOriginalExcelTemplateName(originalFileName);
		project.setProjectHome("https://gitee.com/jerryshensjf/LightSBMEU/");
		project.setBackendProjectName(project.getStandardName());
		project.setStandardName(project.getStandardName()+"FrontEnd");		
		project.setShowBackendProject(showBackendProject);
		project.setUseController(useController);
		project.setUseControllerPrefix(useControllerPrefix);
		project.setTemplatesPath(templatesPath);
		project.setFolderPath(savePath);
		project.generateProjectZip(exportStr);
		return project.getStandardName();
	}
	
	public static String doParseShceuExcel(String excelPath,String excelFileName, String templatesPath,String savePath,boolean showBackendProject,boolean useController,boolean useControllerPrefix, String exportStr, String originalFileName) throws Exception {
		ParseSpreadSheetToProjectAction action = new ParseSpreadSheetToProjectAction();
		FrontProject project = action.parseProject(excelPath,excelFileName);
		project.setOriginalExcelTemplateName(originalFileName);
		project.setProjectHome("https://gitee.com/jerryshensjf/PeaceWingSHCEU/");
		project.setBackendProjectName(project.getStandardName());
		project.setStandardName(project.getStandardName()+"FrontEnd");		
		project.setShowBackendProject(showBackendProject);
		project.setUseController(useController);
		project.setUseControllerPrefix(useControllerPrefix);
		project.setTemplatesPath(templatesPath);
		project.setFolderPath(savePath);
		project.generateProjectZip(exportStr);
		return project.getStandardName();
	}
	
	public static String doParseMjeeeuExcel(String excelPath,String excelFileName, String templatesPath,String savePath,boolean showBackendProject,boolean useController,boolean useControllerPrefix, String exportStr, String originalFileName) throws Exception {
		ParseSpreadSheetToProjectAction action = new ParseSpreadSheetToProjectAction();
		FrontProject project = action.parseProject(excelPath,excelFileName);
		project.setOriginalExcelTemplateName(originalFileName);
		project.setProjectHome("https://gitee.com/jerryshensjf/InfinityJEEEU/");
		project.setBackendProjectName(project.getStandardName());
		project.setStandardName(project.getStandardName()+"FrontEnd");		
		project.setShowBackendProject(showBackendProject);
		project.setUseController(true);
		project.setUseControllerPrefix(useControllerPrefix);
		project.setTemplatesPath(templatesPath);
		project.setFolderPath(savePath);
		project.generateProjectZip(exportStr);
		project.setComputerLanguage("java");
		project.setTechnicalstack("mjeeeu");
		return project.getStandardName();
	}
	
	public static String doParseGoExcel(String excelPath,String excelFileName, String templatesPath,String savePath,boolean showBackendProject,boolean useControllerPrefix, String baseApiUrl, String exportStr,String originalFileName) throws Exception {
		ParseSpreadSheetToProjectAction action = new ParseSpreadSheetToProjectAction();
		FrontProject project = action.parseGoProject(excelPath,excelFileName);
		project.setOriginalExcelTemplateName(originalFileName);
		project.setBackendProjectName(project.getStandardName());
		project.setStandardName(project.getStandardName()+"FrontEnd");		
		project.setShowBackendProject(showBackendProject);
		project.setUseController(true);
		project.setUseControllerPrefix(useControllerPrefix);
		project.setTemplatesPath(templatesPath);
		project.setFolderPath(savePath);
		String pBaseApi = project.getFrontBaseApi();
		if (StringUtil.isBlank(pBaseApi)) project.setFrontBaseApi(baseApiUrl);
		project.generateProjectZip(exportStr);
		project.setComputerLanguage("go");
		return project.getStandardName();
	}
	
	public static String doParseRustExcel(String excelPath,String excelFileName, String templatesPath,String savePath,boolean showBackendProject,boolean useControllerPrefix, String baseApiUrl, String exportStr, String originalFileName) throws Exception {
		ParseSpreadSheetToProjectAction action = new ParseSpreadSheetToProjectAction();
		FrontProject project = action.parseRustProject(excelPath,excelFileName);
		project.setOriginalExcelTemplateName(originalFileName);
		project.setBackendProjectName(project.getStandardName());
		project.setStandardName(project.getStandardName()+"FrontEnd");		
		project.setShowBackendProject(showBackendProject);
		project.setUseController(true);
		project.setUseControllerPrefix(useControllerPrefix);
		project.setTemplatesPath(templatesPath);
		project.setFolderPath(savePath);
		String pBaseApi = project.getFrontBaseApi();
		if (StringUtil.isBlank(pBaseApi)) project.setFrontBaseApi(baseApiUrl);
		project.generateProjectZip(exportStr);
		project.setComputerLanguage("rust");
		project.setTechnicalstack("tower");
		return project.getStandardName();
	}
	
	public static String doParseZigExcel(String excelPath,String excelFileName, String templatesPath,String savePath,boolean showBackendProject,boolean useControllerPrefix, String baseApiUrl, String exportStr, String originalFileName) throws Exception {
		ParseSpreadSheetToProjectAction action = new ParseSpreadSheetToProjectAction();
		FrontProject project = action.parseZigProject(excelPath,excelFileName);
		project.setOriginalExcelTemplateName(originalFileName);
		project.setBackendProjectName(project.getStandardName());
		project.setStandardName(project.getStandardName()+"FrontEnd");		
		project.setShowBackendProject(showBackendProject);
		project.setUseController(true);
		project.setUseControllerPrefix(useControllerPrefix);
		project.setTemplatesPath(templatesPath);
		project.setFolderPath(savePath);
		String pBaseApi = project.getFrontBaseApi();
		if (StringUtil.isBlank(pBaseApi)) project.setFrontBaseApi(baseApiUrl);
		project.generateProjectZip(exportStr);
		project.setComputerLanguage("zig");
		project.setTechnicalstack("light");
		return project.getStandardName();
	}
	
	public static void main(String [] args) throws Exception{
//		ParseSpreadSheetToProjectAction action = new ParseSpreadSheetToProjectAction();
//		FrontProject project = action.parseProject("/home/jerry/git/GatesCore/GatesCore/src/org/javaforever/gatescore/action/GenerateSample.xls");
//		System.out.println(project.getStandardName());
//		for (FrontDomain d: project.getDomains()) {
//			System.out.println(d.getStandardName());
//		}
//		project.generateProjectFiles();
		String excelFolderPath = "C:/Users/jerry.shen03/git/GatesCore/GatesCore/src/org/javaforever/gatescore/action/";
		String excelFileName = "CompleteOracleEn.xls";
		String templatesPath = "C:/Users/jerry.shen03/git/GatesCore/GatesCore/templates/";
		String savePath = "D:/JerryWork/GatesCore/TestBed/";
		doParseExcel(excelFolderPath,excelFileName,templatesPath,savePath,true,false,null,null);
	}
}
